package com.elcom.player;

import java.io.File;

import com.elcom.player.R;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
//import android.widget. LinearLayout;

public final class VideoGallery extends ListActivity  {
	private static final int PLAYSERVER_REQ    = 0x01;		//request code for intent
	private static FileManager 		 flmg;
	private EventHandler handler;
	private EventHandler.TableRow table;
	static Context 		mContext;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mContext = this;
		/*read settings*/	
		//readSettings();
		flmg = new FileManager();
		handler = new EventHandler(VideoGallery.this, flmg,Globals.MANAGER_TYPE_VIDEO);
		table = handler.new TableRow();
		/*sets the ListAdapter for our ListActivity and
		 *gives our EventHandler class the same adapter
		 */
		//handler.setListAdapter(table); 
		setListAdapter(table);
		registerForContextMenu(getListView());
		//	path_label = (TextView)findViewById(R.id.path_label);
		//	detail_label = (TextView)findViewById(R.id.detail_label);
		//	path_label.setText(mContext.getString(R.string.pathis)+ ":" +flmg.getCurrentDir());
		//	path_label.setText(flmg.getCurrentDir());
		//	handler.setUpdateLabel(path_label, detail_label);
//		Intent intent = new Intent(VideoGallery.this, VideoPlayer.class);  
//		intent.putExtra("videofilename", "/mnt/sdcard/test.mp4");
//		startActivity(intent);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case PLAYSERVER_REQ:
			String url = data.getStringExtra("videourl");
			toast(url);
			Intent intent =  new Intent(VideoGallery.this, VideoPlayerActivity.class);
			intent.putExtra("videofilename", url);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stu b
		getMenuInflater().inflate(R.menu.menu_videoplayer, menu);
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method  stub
		switch (item.getItemId()) {
		case R.id.menu_playserver:
			//start activity play video on server
			toast("play-server");
			Intent i =new Intent(VideoGallery.this, playserver.class);
			startActivityForResult(i, PLAYSERVER_REQ);
			return true;
		case R.id.menu_settings:
            return true;
        case R.id.menu_about:
        	return true;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	public void onPause(){
		super.onPause();
	}

	@Override
	public void onDestroy(){
		super.onDestroy();
	}

	/**
	 *  To add more functionality and let the user interact with more
	 *  file types, this is the function to add the ability. 
	 *  
	 *  (note): this method can be done more efficiently 
	 */
	//@Override
	public void onListItemClick(ListView parent, View view, int position, long id) {
		final String item = handler.getData(position);

		File file = new File(flmg.getCurrentDir() + "/" + item);

		if (FileManager.supportedFile(file.getPath())) {
			if(file.exists()){
				String filename =   file.getPath();
				Intent intent = null;
				intent = new Intent(VideoGallery.this, VideoPlayerActivity.class);
				intent.putExtra("videofilename", filename);
				toast(filename);
				startActivity(intent);
			}		
		}
	}
	public void log(String s){
		System.out.println(s);
	}
	public void toast(String ss){
		Toast.makeText(VideoGallery.this, ss, Toast.LENGTH_SHORT).show();
	}
}
